package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"meituan-sys/appconfig"
)

var DB *gorm.DB

func Init() {
	var err error
	redisconfig := appconfig.AppCon.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local",
		redisconfig.User,
		redisconfig.Password,
		redisconfig.Host,
		redisconfig.Port,
		redisconfig.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	if err := DB.AutoMigrate(&User{}); err != nil {
		panic(err)
	}
	fmt.Println("数据库迁移成功")
}
